﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessObjects;

namespace DataObjects.EntityFramework.ModelMapper
{
    /// <summary>
    /// Maps Entity Framework entities to business objects and vice versa.
    /// </summary>
    public class Mapper
    {

        /// <summary>
        /// Maps product category entity to category business object.
        /// </summary>
        /// <param name="entity">A category entity.</param>
        /// <returns>A category business object.</returns>
        internal static Category Map(CategoryEntity entity)
        {
            return new Category
            {
                CategoryId = entity.CategoryId,
                TrafficId = entity.TrafficId,
                SupplierId = (entity.SupplierId == null) ? 0 : (Int32)entity.SupplierId,
                Date = entity.Date,
                Description = entity.Description,
                FirstPoint = entity.FirstPoint,
                LastPoint = entity.LastPoint,
                StartTime = entity.StartTime,
                EndTime = entity.EndTime,
                Status = entity.Status,
                Version = entity.Version.AsBase64String()
            };
        }

        internal static CategoryEntity Map(Category category)
        {
            return new CategoryEntity
            {
                CategoryId = category.CategoryId,
                TrafficId = category.TrafficId,
                SupplierId = category.SupplierId,
                Date = category.Date,
                Description = category.Description,
                FirstPoint = category.FirstPoint,
                LastPoint = category.LastPoint,
                StartTime = category.StartTime,
                EndTime = category.EndTime,
                Status = category.Status,
                //Version = Convert.FromBase64String(category.Version),
            };
        }

        internal static Country Map(CountryEntity entity)
        {
            if (entity == null) return null;

            return new Country
            {
                CountryId = entity.CountryId,
                CountryName = entity.CountryName,
            };
        }

        internal static CountryEntity Map(Country country)
        {
            if (country == null) return null;

            return new CountryEntity
            {
                CountryId = country.CountryId,
                CountryName = country.CountryName,
            };
        }


        /// <summary>
        /// Maps customer entity to customer business object.
        /// </summary>
        /// <param name="entity">A customer entity to be transformed.</param>
        /// <returns>A customer business object.</returns>
        internal static Customer Map(CustomerEntity entity)
        {
            return new Customer
            {
                CustomerId = entity.CustomerId,
                CompanyName = entity.CompanyName,
                Name = entity.Name,
                City = (entity.City == null) ? 0 : (Int32)entity.City,
                Country = (entity.Country == null) ? 0 : (Int32)entity.Country,
                UserName = entity.UserName,
                Password = entity.Password,
                Status = entity.Status,
                Version = entity.Version.AsBase64String(),
                Email = entity.Email,
                Telephone = entity.Telephone,
                MobilePhone = entity.MobilePhone,
                YahooId = entity.YahooId,
                SkypeId = entity.SkypeId,
                GoogleId = entity.GoogleId,
                FacebookId = entity.FacebookId
            };
        }

        internal static CustomerEntity Map(Customer customer)
        {
            return new CustomerEntity
            {
                CustomerId = customer.CustomerId,
                CompanyName = customer.CompanyName,
                Name = customer.Name,
                City = (customer.City == null) ? 0 : (Int32)customer.City,
                Country = (customer.Country == null) ? 0 : (Int32)customer.Country,
                UserName = customer.UserName,
                Password = customer.Password,
                Status = customer.Status,
                //Version = customer.Version.AsBase64String(),
                Email = customer.Email,
                Telephone = customer.Telephone,
                MobilePhone = customer.MobilePhone,
                YahooId = customer.YahooId,
                SkypeId = customer.SkypeId,
                GoogleId = customer.GoogleId,
                FacebookId = customer.FacebookId
            };
        }

        internal static Employee Map(EmployeeEntity entity)
        {
            return new Employee
            {
                EmployeeId = entity.EmployeeId,
                SupplierId = entity.SupplierId,
                Name = entity.Name,
                Picture = entity.Picture,
                Sex = entity.Sex,
                YahooId = entity.YahooId,
                SkypeId = entity.SkypeId,
                GoogleId = entity.GoogleId,
                FacebookId = entity.FacebookId,
                Telephone = entity.Telephone,
                MobilePhone = entity.MobilePhone,
                Status = entity.Status,
                Version = entity.Version.AsBase64String()
            };
        }

        internal static EmployeeEntity Map(Employee employee)
        {
            return new EmployeeEntity
            {
                EmployeeId = employee.EmployeeId,
                SupplierId = employee.SupplierId,
                Name = employee.Name,
                Picture = employee.Picture,
                Sex = employee.Sex,
                YahooId = employee.YahooId,
                SkypeId = employee.SkypeId,
                GoogleId = employee.GoogleId,
                FacebookId = employee.FacebookId,
                Telephone = employee.Telephone,
                MobilePhone = employee.MobilePhone,
                Status = employee.Status,
                //Version = Convert.FromBase64String(employee.Version),

            };
        }

        /// <summary>
        /// Maps order entity to order business object.
        /// </summary>
        /// <param name="entity">An order entity.</param>
        /// <returns>An order business object.</returns>
        internal static Order Map(OrderEntity entity)
        {
            return new Order
            {
                OrderId = entity.OrderId,
                CustomerId = entity.CustomerId,
                SupplierId = (entity.SupplierId == null) ? 0 : (int)entity.SupplierId,
                OrderDate = entity.OrderDate,
                RequiredDate = entity.RequiredDate,
                //ShippedDate = entity.ShippedDate,
                Freight = (entity.Freight == null) ? 0 : (Decimal)entity.Freight,
                Version = entity.Version.AsBase64String(),
                Status = entity.Status,
            };
        }

        internal static OrderEntity Map(Order order)
        {
            return new OrderEntity
            {
                OrderId = order.OrderId,
                CustomerId = order.CustomerId,
                SupplierId = order.SupplierId,
                OrderDate = order.OrderDate,
                RequiredDate = order.RequiredDate,
                //ShippedDate = order.ShippedDate,
                Freight = order.Freight,
                //Version = Convert.FromBase64String(order.Version),
                Status = order.Status,
            };
        }
        /// <summary>
        /// Maps order detail entity to order detail business object.
        /// </summary>
        /// <param name="entity">An order detail entity.</param>
        /// <returns>An order detail business object.</returns>
        internal static OrderDetail Map(OrderDetailEntity entity)
        {
            return new OrderDetail
            {
                ProductName = entity.ProductName,
                OrderId = entity.OrderId,
                CategoryId = entity.CategoryId,
                ProductId = entity.ProductId,
                UnitPrice = entity.UnitPrice,
                Quantity = entity.Quantity,
                Discount = (entity.Discount == null) ? 0 : (Decimal)entity.Discount,
                Version = entity.Version.AsBase64String(),
                Status = entity.Status,
            };
        }

        internal static OrderDetailEntity Map(OrderDetail orderDetail)
        {
            return new OrderDetailEntity
            {
                ProductName = orderDetail.ProductName,
                OrderId = orderDetail.OrderId,
                CategoryId = orderDetail.CategoryId,
                ProductId = orderDetail.ProductId,
                UnitPrice = orderDetail.UnitPrice,
                Quantity = orderDetail.Quantity,
                Discount = orderDetail.Discount,
                //Version = Convert.FromBase64String(orderDetail.Version),
                Status = orderDetail.Status,
            };
        }

        internal static Point Map(PointEntity entity)
        {
            return new Point
            {
                PointId = entity.PointId,
                ProvinceId = entity.ProvinceId,
                Description = entity.Description,
            };
        }

        internal static PointEntity Map(Point point)
        {
            return new PointEntity
            {
                PointId = point.PointId,
                ProvinceId = point.ProvinceId,
                Description = point.Description,
            };
        }

        /// <summary>
        /// Maps product entity to product business object.
        /// </summary>
        /// <param name="entity">A product entity.</param>
        /// <returns>A product business object.</returns>
        internal static Product Map(ProductEntity entity)
        {
            return new Product
            {

                //UnitPrice = (double)entity.UnitPrice,
                ProductId = entity.ProductId,
                CategoryId = entity.CategoryId,
                ChairNumber = entity.ChairNumber,
                UnitPrice = (double)entity.UnitPrice,
                Description = entity.Description,
                Version = entity.Version.AsBase64String(),
                Status = entity.Status,
            };
        }

        /// <summary>
        /// Maps customer entity to customer business object.
        /// </summary>
        /// <param name="entity">A customer entity to be transformed.</param>
        /// <returns>A customer business object.</returns>
        internal static ProductEntity Map(Product product)
        {
            return new ProductEntity
            {

                //UnitPrice = (double)entity.UnitPrice,
                ProductId = product.ProductId,
                CategoryId = product.CategoryId,
                ChairNumber = product.ChairNumber,
                UnitPrice = (decimal)product.UnitPrice,
                Description = product.Description,
                //Version = Convert.FromBase64String(product.Version),
                Status = product.Status,
            };
        }

        internal static Province Map(ProvinceEntity entity)
        {
            return new Province
            {

                ProvinceId = entity.ProvinceId,
                CountryId = entity.CountryId,
                ProvinceName = entity.ProvinceName,
            };
        }

        internal static ProvinceEntity Map(Province province)
        {
            return new ProvinceEntity
            {

                ProvinceId = province.ProvinceId,
                CountryId = province.CountryId,
                ProvinceName = province.ProvinceName,
            };
        }

        internal static Service Map(ServiceEntity entity)
        {
            return new Service
            {

                ServiceId = entity.ServiceId,
                Description = entity.Description,
            };
        }

        internal static ServiceEntity Map(Service service)
        {
            return new ServiceEntity
            {
                ServiceId = service.ServiceId,
                Description = service.Description,
            };
        }


        internal static Supplier Map(SupplierEntity entity)
        {
            return new Supplier
            {
                SupplierId = entity.SupplierId,
                CompanyName = entity.CompanyName,
                UserName=entity.UserName,
                Password=entity.Password,
                Email = entity.Email,
                CompanyPhone = entity.CompanyPhone,
                Mobilephone = entity.MobilePhone,
                YahooId = entity.YahooId,
                SkypeId = entity.SkypeId,
                GoogleId = entity.GoogleId,
                FacebookId = entity.FacebookId,
                Address=entity.Address,
                Description = entity.Description,
                Status = entity.Status
            };
        }


        /// <summary>
        /// Maps customer business object to customer entity.
        /// </summary>
        /// <param name="customer">A customer business object.</param>
        /// <returns>A customer entity.</returns>
        internal static SupplierEntity Map(Supplier supplier)
        {
            return new SupplierEntity
            {
                SupplierId = supplier.SupplierId,
                CompanyName = supplier.CompanyName,
                Email = supplier.Email,
                CompanyPhone = supplier.CompanyPhone,
                MobilePhone = supplier.Mobilephone,
                YahooId = supplier.YahooId,
                SkypeId = supplier.SkypeId,
                GoogleId = supplier.GoogleId,
                FacebookId = supplier.FacebookId,
                UserName = supplier.UserName,
                Password = supplier.Password,
                Address = supplier.Address,
                Description = supplier.Description,
                Status = supplier.Status,
                //Version = Convert.FromBase64String(supplier.Version),
            };
        }

        internal static Traffic Map(TrafficEntity entity)
        {
            return new Traffic
            {
                TrafficId = entity.TrafficId,
                TrafficName = entity.TrafficName,
                SupplierId = entity.SupplierId,
                ServiceId = entity.ServiceId,
                TrafficTypeId = entity.TrafficTypeId,
                DriverId = entity.DriverId,
                SubDriverId1 = (entity.SubDriverId_1 == null) ? 0 : (Int32)entity.SubDriverId_1,
                SubDriverId2 = (entity.SubDriverId_2 == null) ? 0 : (Int32)entity.SubDriverId_2,
                Layer = (entity.Layer == null) ? 0 : (Int32)entity.Layer,
                L1RowNumber = (entity.L1RowNumber == null) ? 0 : (Int32)entity.L1RowNumber,
                L1NCPR = (entity.L1NCPR == null) ? 0 : (Int32)entity.L1NCPR,
                L2RowNumber = (entity.L2RowNumber == null) ? 0 : (Int32)entity.L2RowNumber,
                L2NCPR = (entity.L2NCPR == null) ? 0 : (Int32)entity.L2NCPR,
                Description = entity.Description,
                Picture = entity.Picture,
                //Version = entity.Version.AsBase64String(),
                Status = entity.Status,
            };
        }

        internal static TrafficEntity Map(Traffic traffic)
        {
            return new TrafficEntity
            {
                TrafficId = traffic.TrafficId,
                TrafficName = traffic.TrafficName,
                SupplierId = traffic.SupplierId,
                ServiceId = traffic.ServiceId,
                TrafficTypeId = traffic.TrafficTypeId,
                DriverId = traffic.DriverId,
                SubDriverId_1 = traffic.SubDriverId1,
                SubDriverId_2 = traffic.SubDriverId2,
                Layer = traffic.Layer,
                L1RowNumber = traffic.L1RowNumber,
                L1NCPR = traffic.L1NCPR,
                L2RowNumber = traffic.L2RowNumber,
                L2NCPR = traffic.L2NCPR,
                Description = traffic.Description,
                Picture = traffic.Picture,
                //Version = Convert.FromBase64String(traffic.Version),
                Status = traffic.Status,
            };
        }

        internal static TrafficType Map(TrafficTypeEntity entity)
        {
            return new TrafficType
            {
                TrafficTypeId = entity.TrafficTypeId,
                Description = entity.Description,
            };
        }

        internal static TrafficTypeEntity Map(TrafficType trafficType)
        {
            return new TrafficTypeEntity
            {
                TrafficTypeId = trafficType.TrafficTypeId,
                Description = trafficType.Description,
            };
        }

    }
}
